<template>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form size="small" :model="inputForm" ref="inputForm" :class="method==='view'?'readonly':''"  :disabled="method==='view'" @keyup.enter.native="doSubmit()"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="单位名称" prop="unitName"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.unitName" placeholder="请填写单位名称"     ></el-input> -->
              <el-select v-model="inputForm.unitName" filterable placeholder="请填写单位名称" style="width:100%;" >
                <el-option v-for="item in unitList" :label="item.unitName" :value="item.unitName" :key="item.unitName"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="机构级别" prop="unitLevel"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unitLevel" placeholder="请填写机构级别"    disabled="" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="正处级领导职数" prop="zhengleaderNumber"
                :rules="[
                 ]">
              <el-input v-model="zhengleaderNumber" placeholder="请填写正处级领导职数"   disabled=""  ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="副处级领导职数" prop="fuleaderNumber"
                :rules="[
                 ]">
              <el-input v-model="fuleaderNumber" placeholder="请填写副处级领导职数"   disabled=""  ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="科室名称" prop="departName"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.departName" placeholder="请填写科室名称"     ></el-input> -->
              <el-select v-model="inputForm.departName" filterable placeholder="请填写科室名称" style="width:100%;" >
                <el-option v-for="item in departList" :label="item.departName" :value="item.departName" :key="item.departName"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="科级领导职数" prop="departLeaderNum"
                :rules="[
                 ]">
              <el-input v-model="inputForm.departLeaderNum" placeholder="请填写科级领导职数"  disabled=""   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="行政级别" prop="adminisLevel"
                :rules="[
                 ]">
              <!-- <el-input v-model="inputForm.adminisLevel" placeholder="请填写行政级别"     ></el-input> -->
              <el-select v-model="inputForm.adminisLevel" filterable placeholder="请填写行政级别" style="width:100%;" >
                <el-option v-for="item in adminstList" :label="item.administLevel" :value="item.administLevel" :key="item.administLevel"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" v-if="method === 'add'" type="primary" @click="continueDoSubmit()">继续添加</el-button>
      <el-button size="small" @click="visible = false">关闭</el-button>
      <el-button size="small" v-if="method !== 'view'" type="primary" @click="doSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        oldInputForm: '',
        unitList: [],
        departList: [],
        zhengleaderNumber: '',
        fuleaderNumber: '',
        adminstList: [],
        inputForm: {
          id: '',
          unitName: '',
          unitLevel: '',
          leaderNumber: '',
          departName: '',
          departLeaderNum: '',
          adminisLevel: '',
          sort: '',
          remarks: ''
        }
      }
    },
    components: {
    },
    watch: {
      'inputForm.unitName': {
        handler (newVal) {
          for (let i in this.unitList) {
            if (this.unitList[i].unitName === newVal) {
              this.inputForm.unitLevel = this.unitList[i].unitLevel
              this.zhengleaderNumber = this.unitList[i].leaderNumber !== undefined ? this.unitList[i].leaderNumber.split(',')[0] : ''
              this.fuleaderNumber = this.unitList[i].leaderNumber !== undefined ? this.unitList[i].leaderNumber.split(',')[1] : ''
              this.$http.get(`/basicdata/basicunit/basicUnit/queryById?id=${this.unitList[i].id}`).then(({data}) => {
                this.departList = data.basicUnit.basicUnitDepartList
              })
              break
            }
          }
        },
        immediate: true,
        deep: false
      },
      'inputForm.departName': {
        handler (newVal) {
          for (let i in this.departList) {
            if (this.departList[i].departName === newVal) {
              this.inputForm.departLeaderNum = this.departList[i].departLeaderNum
              break
            }
          }
        },
        immediate: true,
        deep: false
      }
    },
    methods: {
      init (method, obj) {
        this.method = method
        this.zhengleaderNumber = ''
        this.fuleaderNumber = ''
        this.unitGetData()
        this.adminstGetData()
        if (method === 'add') {
          this.title = `新建人员单位行政`
        } else if (method === 'edit') {
          this.title = '修改人员单位行政'
        } else if (method === 'view') {
          this.title = '查看人员单位行政'
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = ''
          this.oldInputForm = ''
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.oldInputForm = obj
            this.inputForm = JSON.parse(JSON.stringify(obj))
          }
        })
      },
      unitGetData () {
        this.unitList = []
        this.$http({
          url: '/basicdata/basicunit/basicUnit/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 9999
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.unitList = data.page.list
          }
        })
      },
      adminstGetData () {
        this.adminstList = []
        this.$http({
          url: '/basicdata/basicadminist/basicAdminist/list',
          method: 'get',
          params: {
            'pageNo': 1,
            'pageSize': 9999
          }
        }).then(({data}) => {
          if (data && data.success) {
            this.adminstList = data.page.list
          }
        })
      },
   // 表单提交
      doSubmit () {
        this.inputForm.leaderNumber = this.zhengleaderNumber + ',' + this.fuleaderNumber
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.visible = false
          }
        })
      },
      continueDoSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.zhengleaderNumber = ''
            this.fuleaderNumber = ''
            this.$emit('addRow', this.oldInputForm, JSON.parse(JSON.stringify(this.inputForm)))
            this.$refs['inputForm'].resetFields()
          }
        })
      }
    }
  }
</script>
